package it.univaq.mwt.model.entity;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;

import org.hibernate.annotations.IndexColumn;


/*
--
-- Struttura della tabella `gift`
--

CREATE TABLE IF NOT EXISTS `gift` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_category` int(11) NOT NULL,
  `link` varchar(100) NOT NULL,
  `link_name` varchar(100) NOT NULL,
  `price` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=447 ;
--
*/
@Entity 
@NamedQueries({ 
	@NamedQuery(name="getGiftFromId",query="SELECT g  FROM Gift g WHERE g.id=:id_gift"),
	@NamedQuery(name="updateGift",query="UPDATE Gift g SET g.link=:site ,g.link_name=:title , g.description=:des WHERE g.id=:gift_id "),
	@NamedQuery(name="searchNumGift", query="SELECT COUNT(g) FROM Gift g WHERE g.id_category!=1 "),
	@NamedQuery(name="getListGift", query="SELECT g FROM Gift g WHERE g.id_category!=1 ORDER BY g.id"),
	@NamedQuery(name="getListGiftSmall", query="SELECT g FROM Gift g WHERE ROWNUM <=:limit "),
	@NamedQuery(name="getListGiftByIdList", query="SELECT g FROM Gift g JOIN g.list l WHERE l.id=:id_list"),
	@NamedQuery(name="getListGiftByIdCategory", query="SELECT g FROM Gift g JOIN g.category c WHERE c.id=:id_category"),
	
	
})
@SequenceGenerator(name="sequence",sequenceName="MASTER.GIFT_SEQ")
public class Gift implements Serializable {
	
	@Id
	@GeneratedValue(generator="sequence")
   	@Column(name="id_gift",nullable=false,length=11)
	private int id;
	
	@Column(name="id_category",nullable=true,length=11,insertable=false, updatable=false)
	private int id_category;
	
	@Column(name="link",nullable=true,length=100)
	private String link;
	
	@Column(name="link_name",nullable=false,length=100)
	private String link_name;
	
	@Column(name="name",nullable=true,length=100)
	private String name;
	
	@Column(name="price",nullable=true,length=11)
	private int price;
		
	@Lob
	@Column(name="description",nullable=true)
	private String description;
	
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH})
	  @JoinColumn(name="id_category", referencedColumnName="id_category")
	private Category category;
	
	
	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY) 
	@JoinTable(name="gift_list",
			joinColumns = {@JoinColumn(name="id_gift", referencedColumnName="id_gift", nullable=true)},
			inverseJoinColumns={@JoinColumn(name="id_list", referencedColumnName="id_list", nullable=true)})
	private Set<Lists> list;
	
	/*@OneToOne(cascade = { CascadeType.MERGE,CascadeType.REMOVE, CascadeType.REFRESH},optional=true)
	@JoinTable(name="files_gift",
			joinColumns = {@JoinColumn(name="id_gift", referencedColumnName="id_gift", nullable=true)},
			inverseJoinColumns={@JoinColumn(name="id_file", referencedColumnName="id_file", nullable=true)})*/
	@OneToOne(cascade = { CascadeType.MERGE,CascadeType.REMOVE, CascadeType.REFRESH})
	@JoinColumn(name="id_file", referencedColumnName="id_file")
	private Files files;
	public Files getFiles() {
		return files;
	}
	public void setFiles(Files files) {
		this.files = files;
	}
	
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId_category() {
		return id_category;
	}
	public void setId_category(int id_category) {
		this.id_category = id_category;
	}
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	public String getLink_name() {
		return link_name;
	}
	public void setLink_name(String link_name) {
		this.link_name = link_name;
	}
/*	public Set<Category> getCategory() {
		return category;
	}
	public void setCategory(Set<Category> category) {
		this.category = category;
	}*/
	public Set<Lists> getList() {
		return list;
	}
	public void setList(Set<Lists> list) {
		this.list = list;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}
